package api

import (
	"gitee.com/DengAnbang/Wefore/src/bean"
	"gitee.com/DengAnbang/Wefore/src/dbops"
	"gitee.com/DengAnbang/Wefore/src/fun"
	"gitee.com/DengAnbang/goutils/httpUtils"
	"net/http"
)

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 用户注册
* @description 用户注册的接口
* @method socket type=101
* @url /pc/user/register
* @param account 必选 string 登陆名
* @param pwd 必选 string 密码
* @param name 必选 string 用户昵称
* @param jobNumber 选填 string 用户工号
* @param phoneNumber 选填 string 用户电话
* @param headPortrait 选填 string 用户头像
* @param credentialType 选填 string 证件类型
* @param credentialNumber 选填 string 证件号码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":UserBean}
* @remark 最后修改时间:2018.12.07 10.30
* @number 1
 */
func UserRegisterHttp(_ http.ResponseWriter, r *http.Request) error {
	account := httpUtils.GetValueFormRequest(r, "account")
	pwd := httpUtils.GetValueFormRequest(r, "pwd")
	name := httpUtils.GetValueFormRequest(r, "name")
	jobNumber := httpUtils.GetValueFormRequest(r, "jobNumber")
	phoneNumber := httpUtils.GetValueFormRequest(r, "phoneNumber")
	headPortrait := httpUtils.GetValueFormRequest(r, "headPortrait")
	credentialType := httpUtils.GetValueFormRequest(r, "credentialType")
	credentialNumber := httpUtils.GetValueFormRequest(r, "credentialNumber")
	userBean := dbops.UserBean{
		BaseUser: dbops.BaseUser{
			UserName:     name,
			HeadPortrait: headPortrait,
		},
		Account:          account,
		JobNumber:        jobNumber,
		PhoneNumber:      phoneNumber,
		CredentialType:   credentialType,
		CredentialNumber: credentialNumber,
		Pwd:              pwd,
	}
	user, err := dbops.UserRegister(userBean)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(user)
}

func UserRegisterSocket(parameters map[string]interface{}) error {
	account := fun.GetString(parameters, "account")
	pwd := fun.GetString(parameters, "pwd")
	name := fun.GetString(parameters, "name")
	jobNumber := fun.GetString(parameters, "jobNumber")
	phoneNumber := fun.GetString(parameters, "phoneNumber")
	headPortrait := fun.GetString(parameters, "headPortrait")
	credentialType := fun.GetString(parameters, "credentialType")
	credentialNumber := fun.GetString(parameters, "credentialNumber")
	userBean := dbops.UserBean{
		BaseUser: dbops.BaseUser{
			UserName:     name,
			HeadPortrait: headPortrait,
		},
		Account:          account,
		JobNumber:        jobNumber,
		PhoneNumber:      phoneNumber,
		CredentialType:   credentialType,
		CredentialNumber: credentialNumber,
		Pwd:              pwd,
	}
	user, err := dbops.UserRegister(userBean)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(user)
}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 用户登陆
* @description 用户登陆的接口
* @method socket type=102
* @url /pc/user/login
* @param account 必选 string 登陆名
* @param pwd 必选 string 密码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":UserBean}
* @remark http请求登录成功后，需要用socket发送一个 type为100，参数为userId的消息，使用socket登录则不需要
* @number 2
 */
func UserLoginHttp(_ http.ResponseWriter, r *http.Request) error {
	account := httpUtils.GetValueFormRequest(r, "account")
	pwd := httpUtils.GetValueFormRequest(r, "pwd")
	user, err := dbops.UserLogin(account, pwd)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(user)

}

func UserLoginSocket(parameters map[string]interface{}) (string, error) {
	account := fun.GetString(parameters, "account")
	pwd := fun.GetString(parameters, "pwd")
	user, err := dbops.UserLogin(account, pwd)
	if err != nil {
		return "", err
	}
	return user.UserId, bean.NewSucceedMessage(user)

}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 用户修改
* @description 用户修改的接口
* @method socket type=103
* @url /pc/user/update
* @param account 必选 string 登陆名
* @param pwd 选填 string 密码
* @param name 选填 string 用户昵称
* @param jobNumber 选填 string 用户工号
* @param phoneNumber 选填 string 用户电话
* @param headPortrait 选填 string 用户头像
* @param credentialType 选填 string 证件类型
* @param credentialNumber 选填 string 证件号码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"修改成功"}
* @remark 最后修改时间:2018.12.07 10.30
* @number 3
 */
func UserUpdateHttp(_ http.ResponseWriter, r *http.Request) error {
	account := httpUtils.GetValueFormRequest(r, "account")
	pwd := httpUtils.GetValueFormRequest(r, "pwd")
	name := httpUtils.GetValueFormRequest(r, "name")
	jobNumber := httpUtils.GetValueFormRequest(r, "jobNumber")
	phoneNumber := httpUtils.GetValueFormRequest(r, "phoneNumber")
	headPortrait := httpUtils.GetValueFormRequest(r, "headPortrait")
	credentialType := httpUtils.GetValueFormRequest(r, "credentialType")
	credentialNumber := httpUtils.GetValueFormRequest(r, "credentialNumber")
	userBean := dbops.UserBean{
		BaseUser: dbops.BaseUser{
			UserName:     name,
			HeadPortrait: headPortrait,
		},
		Account:          account,
		JobNumber:        jobNumber,
		PhoneNumber:      phoneNumber,
		CredentialType:   credentialType,
		CredentialNumber: credentialNumber,
		Pwd:              pwd,
	}
	err := dbops.UserUpdate(userBean)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("修改成功")
}
func UserUpdateSocket(parameters map[string]interface{}) error {
	account := fun.GetString(parameters, "account")
	pwd := fun.GetString(parameters, "pwd")
	name := fun.GetString(parameters, "name")
	jobNumber := fun.GetString(parameters, "jobNumber")
	phoneNumber := fun.GetString(parameters, "phoneNumber")
	headPortrait := fun.GetString(parameters, "headPortrait")
	credentialType := fun.GetString(parameters, "credentialType")
	credentialNumber := fun.GetString(parameters, "credentialNumber")
	userBean := dbops.UserBean{
		BaseUser: dbops.BaseUser{
			UserName:     name,
			HeadPortrait: headPortrait,
		},
		Account:          account,
		JobNumber:        jobNumber,
		PhoneNumber:      phoneNumber,
		CredentialType:   credentialType,
		CredentialNumber: credentialNumber,
		Pwd:              pwd,
	}
	err := dbops.UserUpdate(userBean)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("修改成功")
}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 删除用户
* @description 删除用户的接口
* @method socket type=108
* @param account 必选 string 登陆名
* @url /pc/user/delete
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"删除成功"}
* @remark 最后修改时间:2018.12.07 10.30
* @number 8
 */
func UserDeleteHttp(_ http.ResponseWriter, r *http.Request) error {
	account := httpUtils.GetValueFormRequest(r, "account")
	err := dbops.UserDelete(account)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}

func UserDeleteSocket(parameters map[string]interface{}) error {
	account := fun.GetString(parameters, "account")
	err := dbops.UserDelete(account)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 查询所有用户
* @description 用户修改的接口
* @method socket type=106
* @url /pc/user/select/all
* @return {"code":0,"type":0,"message":"","debug_message":"","data":[]UserBean}
* @remark 最后修改时间:2018.12.07 10.30
* @number 6
 */
func UserSelectAllHttp(_ http.ResponseWriter, r *http.Request) error {
	beans, err := dbops.UserSelectAll()
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)

}
func UserSelectAllSocket(parameters map[string]interface{}) error {
	beans, err := dbops.UserSelectAll()
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)

}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 查询用户信息
* @description 查询用户信息的接口
* @method socket type=107
* @url /pc/user/select
* @return {"code":0,"type":0,"message":"","debug_message":"","data":UserBean}
* @remark 最后修改时间:2018.12.07 10.30
* @number 7
 */
func UserSelectByUserIdHttp(_ http.ResponseWriter, r *http.Request) error {
	userId := httpUtils.GetValueFormRequest(r, "userId")
	beans, err := dbops.UserSelectByUserId(userId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)

}
func UserSelectByUserIdSocket(parameters map[string]interface{}) error {
	userId := fun.GetString(parameters, "userId")
	beans, err := dbops.UserSelectByUserId(userId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 用户添加到组织机构中
* @description 用户添加到组织机构中的接口
* @method socket type=104
* @url /pc/user/to/structure/tree
* @param nodeId 必填 string 组织机构节点id
* @param userId 必填 string 用户的id，多个权限用”,”隔开
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"添加成功"}
* @remark 这里添加的时候，会清空以前的信息，比如 已经存在1了，想继续添加2，则传1，2，如果只传了2，则表示删除1，添加2
* @number 4
 */
func UserToStructureTreeAddHttp(_ http.ResponseWriter, r *http.Request) error {
	nodeId := httpUtils.GetValueFormRequest(r, "nodeId")
	userId := httpUtils.GetValueFormRequest(r, "userId")
	err := dbops.UserToStructureTreeAdd(nodeId, userId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("添加成功")
}

func UserToStructureTreeAddSocket(parameters map[string]interface{}) error {
	nodeId := fun.GetString(parameters, "nodeId")
	userId := fun.GetString(parameters, "userId")
	err := dbops.UserToStructureTreeAdd(nodeId, userId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("添加成功")
}

/**
* showdoc
* @catalog 接口文档/用户相关
* @title 查询组织机构中所有的人员
* @description 查询组织机构中，所有的人员的接口
* @method socket type=105
* @url /pc/user/to/structure/tree/select/all
* @param nodeId 必填 string 组织机构节点id
* @return {"code":0,"type":0,"message":"","debug_message":"","data":[]UserBean}
* @remark 最后修改时间:2018.12.07 10.30
* @number 5
 */
func UserToStructureTreeSelectAllHttp(_ http.ResponseWriter, r *http.Request) error {
	nodeId := httpUtils.GetValueFormRequest(r, "nodeId")
	beans, err := dbops.UserToStructureTreeSelectAll(nodeId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}

func UserToStructureTreeSelectAllSocket(parameters map[string]interface{}) error {
	nodeId := fun.GetString(parameters, "nodeId")
	beans, err := dbops.UserToStructureTreeSelectAll(nodeId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}
